<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeFn">
    <!--
        1、给form组件添加model属性
        2、给form组件绑定rules
        3、给form-item绑定prop属性
        4、v-model双向绑定表单
     -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="分类">
        <!-- 分类下拉列表 -->
        <el-select v-model="formData.category" class="select" placeholder="全部分类">
          <el-option :value="''">全部分类</el-option>
          <el-option
            v-for="item in categoriesData"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经纪">
        <el-select v-model="formData.agent" class="select" placeholder="全部经纪">
          <el-option :value="''">全部经纪</el-option>
          <el-option
            v-for="item in agentData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入标题" />
      </el-form-item>
      <!-- 标签 -->
      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 添加标签</el-button>
      </el-form-item>

      <el-form-item label="地址">
        <!-- <el-select v-model="formData.selectArea" placeholder="选择区域">
          <el-option
            :label="''"
            :value="''"
          />
        </el-select> -->
        <el-input v-model="formData.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" placeholder="请输入价格" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="formData.desc"
          type="textarea"
          :rows="4"
          placeholder="请输入描述内容"
        />
      </el-form-item>

      <!-- 介绍图片 -->
      <el-form-item label="介绍图片">
        <!-- 放置上传图片 -->
        <UpImg ref="img1" />
      </el-form-item>

      <!-- 封面 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="封面">
            <!-- 放置上传图片 -->
            <UploadImage ref="img2" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addPropertiesList } from '@/api/properties'
import { getCategories, getAgents, editPropertiesList } from '@/api/properties'
import UpImg from './upImg.vue'
export default {
  components: { UpImg },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        image: '', // 封面
        title: '', // 标题
        tags: [], // 标签
        address: '', // 地址
        category: '', // 分类
        agent: '', // 经纪
        price: '', // 价格
        desc: '', // 介绍
        photos: [] // 图片
      },
      selectArea: '',
      rules: {
        title: [
          { required: true, message: '标题不能为空！', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空！', trigger: 'blur' }
        ]
      },
      // 标签
      inputVisible: false,
      inputValue: '',
      // 标签，
      // 分类
      categoriesData: [],
      // 经纪
      agentData: []

    }
  },
  watch: {
    showDialog(val) {
      this.$nextTick(() => {
        if (val === true) {
          console.log(this.formData.id)
          if (this.formData.id) {
            this.$refs.img2.imgUrl = this.formData.image
            this.$refs.img1.fileList = this.formData.photos.map(v => {
              return { 'url': v }
            })
            console.log(this.$refs.img1.imgUrl, 8888)
          }
        }
      })
    }
  },
  async created() {
    // 分类列表
    const resCategories = await getCategories()
    // 经纪人列表
    const resAgents = await getAgents()
    this.agentData = resAgents.data
    this.categoriesData = resCategories.data
  },
  methods: {
    // 确定
    async submit() {
      await this.$refs.form.validate()
      // 保存封面的图片地址
      this.formData.image = this.$refs.img2.imgUrl
      // 保存介绍图片的地址
      this.formData.photos = this.$refs.img1.fileList.map(v => {
        return v.url
      })
      console.log(999, this.formData.photos)
      // 更新
      if (this.formData.id) {
        console.log(this.$refs.img1.imgUrl)
        await editPropertiesList(this.formData)
      } else {
        // 新增

        // 调用接口
        await addPropertiesList(this.formData)
      }
      // 更新列表
      this.$emit('updateEvent')
      // 关闭弹窗
      this.closeFn()
    },
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        image: '', // 封面
        title: '', // 标题
        tags: [], // 标签
        address: '', // 地址
        category: '', // 分类
        agent: '', // 经纪
        price: '', // 价格
        desc: '', // 介绍
        photos: [] // 图片
      }
      // 清空校验规则
      this.$refs.form.resetFields()
      this.$refs.img2.removeImg()
      this.$refs.img1.fileList = []
    },

    //
    //
    //
    //
    // 标签
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }

  }
}
</script>
<style lang="scss" scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
